package com.seari.controller;

import com.seari.bean.EtMaintainPlan;

import com.seari.bean.EtMaintainRecords;
import com.seari.pojo.EtmaintainPlanParam;
import com.seari.pojo.MessageBean;
import com.seari.service.EtMaintainPlanService;
import com.seari.utils.StringHelper;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;

//设备维护计划Controller

@Controller
@RequestMapping("et/maintain")
public class EtMaintainController {

    @Autowired
    private EtMaintainPlanService etMaintainPlanService;

    /**
     * //查询设备维护计划数据
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "planList")
    @ResponseBody
    public MessageBean getEtmaintainPlan(@RequestBody EtmaintainPlanParam param) {

        MessageBean message = new MessageBean("0", null, "成功");
        if (null != param) {
            if (null != param.getEid() && param.getEid() != "") {
                List<EtMaintainPlan> lists = etMaintainPlanService.getEtmaintainPlan(param);
                message.setData(lists);
            } else {
                List<EtMaintainPlan> lists = etMaintainPlanService.getEtmaintainPlans(param);
                message.setData(lists);
            }

        }
        return message;

    }

    /**
     * //新增设备维修计划
     *
     * @param et
     * @return
     */
    @PostMapping(value = "addMaintain")
    @ResponseBody
    public MessageBean addMaintain(@RequestBody EtMaintainPlan et) {
        MessageBean mb = new MessageBean();
        String stationName = et.getStationName();
        String eventName = et.getEventName();
        String maintainthisTime = et.getMaintainthisTime();
        String maintainnextTime = et.getMaintainnextTime();
        if (StringUtils.isBlank(stationName) || StringUtils.isBlank(eventName) || StringUtils.isBlank(maintainthisTime) || StringUtils.isBlank(maintainnextTime)) {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }
        int resultNum = 0;
        resultNum = etMaintainPlanService.addMaintain(et);
        if (resultNum > 0) {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("新增设备维护计划成功");
        } else {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("新增设备维护计划失败");
        }
        return mb;
    }


    /**
     * //更新设备维修计划
     *
     * @param et
     * @return
     */
    @PostMapping(value = "updateMaintain")
    @ResponseBody
    public MessageBean updateMaintain(@RequestBody EtMaintainPlan et) {
        MessageBean mb = new MessageBean();
//        String maintainthisTime = et.getMaintainthisTime();
//        String maintainnextTime = et.getMaintainnextTime();
//        String taskNum = et.getTaskNum();
//        if(StringUtils.isBlank(taskNum)||StringUtils.isBlank(maintainthisTime)||StringUtils.isBlank(maintainnextTime)){
//            //表单验证
//            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
//            mb.setMessage("表单信息不完整");
//            return mb;
//        }
        int resultNum = 0;
        resultNum = etMaintainPlanService.updateMaintain(et);
        if (resultNum > 0) {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("更新成功");
        } else {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("更新失败");
        }

        return mb;
    }

    /**
     * //删除设备维修计划
     *
     * @param taskNum
     * @return
     */
    @RequestMapping(value = "deleteMaintain/{taskNum}")
    @ResponseBody
    public MessageBean deleteMaintain(@PathVariable("taskNum") String taskNum) {
        MessageBean mb = new MessageBean();
        int resultNum = 0;
        resultNum = etMaintainPlanService.deleteMaintain(taskNum);
        if (resultNum > 0) {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("删除成功");
        } else {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("删除失败");
        }
        return mb;
    }


    /**
     * //根据专业，系统，设备类型查询设备维修记录
     *
     * @param doMain
     * @param systemName
     * @param eventType
     * @return
     */
    @RequestMapping(value = "getRecord/{doMain}/{systemName}/{eventType}")
    @ResponseBody
    public MessageBean getRecord(@PathVariable("doMain") String doMain, @PathVariable("systemName") String systemName, @PathVariable("eventType") String eventType) {
        MessageBean mb = new MessageBean();
        List<EtMaintainRecords> list = etMaintainPlanService.getRecord(doMain, systemName, eventType);

        mb.setData(list);
        return mb;
    }

    /**
     * 添加设备检修记录
     *
     * @param etm
     * @return
     */
    @RequestMapping(value = "addRecord")
    @ResponseBody
    public MessageBean addRecord(@RequestBody EtMaintainRecords etm) {
        MessageBean mb = new MessageBean();

        int resultNum = 0;
        resultNum = etMaintainPlanService.addRecord(etm);

        if (resultNum > 0) {
            mb.setMessage("添加成功");
        } else {
            mb.setMessage("添加失败");
        }

        return mb;
    }


    /**
     * 设备检修记录查询
     *
     * @return
     */
    @RequestMapping(value = "getRecords")
    @ResponseBody
    public MessageBean getRecords(@RequestBody EtmaintainPlanParam param) {
        MessageBean mb = new MessageBean();
        List<EtMaintainRecords> list = etMaintainPlanService.getRecords(param);

        mb.setData(list);
        return mb;
    }


    /**
     * 更新设备检修记录
     *
     * @param etm
     * @return
     */
    @RequestMapping(value = "updateRecords")
    @ResponseBody
    public MessageBean updateRecords(@RequestBody EtMaintainRecords etm) {
        MessageBean mb = new MessageBean();
        int resultNum = 0;

        resultNum = etMaintainPlanService.updateRecords(etm);

        if (resultNum > 0) {
            mb.setMessage("更新成功");
        } else {
            mb.setMessage("更新失败");
        }
        return mb;
    }

    /**
     * 设备维修记录单个删除
     *
     * @param taskNum
     * @return
     */
    @RequestMapping(value = "deleteRecord/{taskNum}")
    @ResponseBody
    public MessageBean deleteRecord(@PathVariable("taskNum") String taskNum) {
        MessageBean mb = new MessageBean();
        int resultNum = 0;
        resultNum = etMaintainPlanService.deleteRecord(taskNum);

        if (resultNum > 0) {
            mb.setMessage("删除成功");
        } else {
            mb.setMessage("删除失败");
        }

        return mb;

    }


    /**
     * 根据工单号数组批量删除设备维护记录
     *
     * @param taskNums
     * @return
     */
    @RequestMapping(value = "deleteRecords")
    @ResponseBody
    public MessageBean deleteRecords(@RequestParam(value = "taskNums") String[] taskNums) {
        MessageBean mb = new MessageBean();

        Integer result = 0;
        if(taskNums.length>0){
            result = etMaintainPlanService.deleteRecords(taskNums);
            if (result > 0) {
                mb.setMessage("删除成功");
            } else {
                mb.setMessage("删除失败");
            }
        }else {

            mb.setMessage("请选中需要删除的数据");
        }

        return mb;
    }

}
